#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <errno.h>

#include <iostream>
#include <zlib.h>
#include <string>

using std::cout;
using std::endl;
using std::string;

int show_usage(int argc, char * argv[]) {
	cout << "usage:" << argv[0] << " file_path" << endl;
	return 1;
}

int main(int argc, char * argv[]) {
	if (2 != argc) { show_usage(argc, argv); return -1; }
	string file_path = argv[1];
	struct stat s;
	int ret = stat(file_path.c_str(), &s);
	if (0 != ret) { cout << "stat file:" << file_path << ", errno:" << errno << endl; return -2; }
	char * buf = new char[s.st_size];
	FILE * fp = fopen(file_path.c_str(), "r");
	fread(buf, s.st_size, 1, fp);
	fclose(fp);
	cout << "crc begin" << endl;
	for (int i = 0; i < s.st_size; i+= 1024 * 1024) {
		int len = 1024 * 1024; 
		int left = s.st_size - i;
		if (left < 1024 * 1024) len = left;
		uLong crc_init = crc32(0L, NULL, 0);
       		long long crc = crc32(crc_init, (Bytef *)&buf[i], len);
		cout << crc << endl;
     	}
	cout << "crc end" << endl;
	delete[] buf;
	return 0;
}
